﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using dotNET.Application.Infrastructure;
using dotNET.Data;
using dotNET.Domain.Entities;
using dotNET.Repository;
using dotNET.Dto;

namespace dotNET.Application.App
{
    public class ModuleButtonApp : App, IModuleButtonApp
    {
        public IRepositoryBase<ModuleButton> _moduleButtonRep { get; set; }

        /// <summary>
        /// Saas模块按钮  
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        public async Task<List<ModuleButton>> GetSaasModuleListAsync(ModuleButtonOption option = null)
        {
            return await GetListAsync( option);
        }

        /// <summary>
        /// 代理模块按钮
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        public async Task<List<ModuleButton>> GetAgentModuleListAsync(ModuleButtonOption option = null)
        {
            return await GetListAsync( option);
        }

        private async Task<List<ModuleButton>> GetListAsync( ModuleButtonOption option)
        {
            Sql where = Sql.Builder;
           
            if (option != null)
            {
                if (option.ModuleId.HasValue && option.ModuleId != 0)
                {
                    where = where.Where("`ModuleId`=@ModuleId", new { ModuleId = option.ModuleId.Value });
                }
                if (option.ParentId.HasValue)
                {
                    where = where.Where("`ParentId`=@ParentId", new { ParentId = option.ParentId.Value });
                }
            }
            return (await _moduleButtonRep.GetListAsync(where.SQL, where.Arguments)).ToList();
        }

        public async Task<ModuleButton> GetAsync(long id)
        {
            return await _moduleButtonRep.GetAsync(id);
        }

        public async Task<R> DeleteAsync(long id)
        {
            if (await _moduleButtonRep.RecordCountAsync(new { ParentId = id }) > 0)
            {
                return R.Err(msg: "删除失败！操作的对象包含了下级数据。");
            }

            if (await _moduleButtonRep.DeleteAsync(id))
            {
                return R.Suc();
            }

            return R.Err();
        }

        public async Task<R> DeleteListAsync(long moduleId)
        {
            if (await _moduleButtonRep.DeleteListAsync(new { ModuleId = moduleId }))
            {
                return R.Suc();
            }
            return R.Err();
        }

        public async Task<R> CreateAsync(ModuleButton moduleButton)
        {
            moduleButton.Id = moduleButton.CreateId();
            moduleButton.CreatorTime = DateTime.Now;
          
            if (await _moduleButtonRep.InsertAsync(moduleButton) > 0)
                return R.Suc();

            return R.Err();
        }

        public async Task<R> UpdateAsync(ModuleButton moduleButton)
        {
            if (await _moduleButtonRep.UpdateAsync(moduleButton))
                return R.Suc();

            return R.Err();
        }
    }
}
